#!/usr/bin/python
# coding:utf-8
__author__ = 'Methew Pan'
import os
from fabric.api import *
from fabric.context_managers import *
from fabric.contrib.console import confirm
from fabric.contrib.files import *
import server as serverfile
__create__time = '20180430'

path = os.getcwd()
if os.name == 'posix':
    actPath = os.path.join(path, '.env/bin/activate')
else:
    actPath = os.path.join(path, '.env\\Scripts\\activate.bat')
# 执行命令的服务器
env.roledefs = serverfile.roledefs
# 使用远程命令的用户名
env.port = '22'
env.user = "root"
env.sudo_user = "root"
env.disable_known_hosts = True
env.warn_only = False
# env.command_timeout = 25
env.connection_attempts = 2

env.passwords = serverfile.passwords

env.source_dir = serverfile.source_dir




@task
@roles('all')
def test():
    run('ifconfig -a')


def pack():
    # build the package
    local('python setup.py sdist --formats=gztar', capture=False)


def deploy():
    # figure out the package name and version
    dist = local('python setup.py --fullname', capture=True).strip()
    filename = '%s.tar.gz' % dist

    # upload the package to the temporary folder on the server
    put('dist/%s' % filename, '/tmp/%s' % filename)

    # install the package in the application's virtualenv with pip
    run('/usr/local/melog/melog-backend/FlaskEnv/bin/pip install /tmp/%s' % filename)

    # remove the uploaded package
    run('rm -r /tmp/%s' % filename)

    # touch the .wsgi file to trigger a reload in mod_wsgi
    run('touch /var/www/yourapplication.wsgi')


def pip():
    local("source " + actPath)
    local("pip install -r requirements.txt")


def gitPush():
    os.chdir(os.pardir)
    local("pwd")
    local("git add . ")
    local("git commit -m 'change' ")
    local("git push")


def gitPulls():
    run("cd /usr/local/melog")
    run("pwd")
    run("git pull")


def actM():
    local("source " + actPath)


def runM():
    local("source " + actPath)
    local("python manage.py runserver")


# E:\Project\melog\melog-backend\.env\Scripts\activate.bat


def run():
    local("python manage.py runserver")


def initdb():
    # local("source " + actPath)
    local("python manage.py db init")
    local("python manage.py db migrate")
    local("python manage.py db upgrade")


def updb():
    # local("source " + actPath)
    local("python manage.py db migrate")
    local("python manage.py db upgrade")


def data():
    local("source " + actPath)
    local("python manage.py data")
